<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\AssetsWorthTrend;
use App\Admin\Metrics\BannerMyAssetsWorth;
use App\Admin\Metrics\BannerMyTodoCounts;
use App\Admin\Metrics\BannerServiceIssueCounts;
use App\Admin\Metrics\BannerMaintenanceRecordCounts;
use App\Admin\Metrics\CustomerTrend;
use App\Admin\Metrics\OrderTrend;
use App\Admin\Metrics\WorthTrend;
use App\Http\Controllers\Controller;
use App\Models\AdminUser;
use App\Models\MessageRecord;
use App\Models\OrderServiceProfile;
use App\Models\OrderServiceSubmitDetail;
use App\Models\User;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Card;

class HomeController extends Controller
{
    // public function index(Content $content)
    // {
    //     return $content
    //         ->header('仪表台')
    //         ->description('仪表台')
    //         ->body(function (Row $row) {
    //             $row->column(6, function (Column $column) {
    //                 $column->row(Dashboard::title());
    //                 $column->row(new Examples\Tickets());
    //             });

    //             $row->column(6, function (Column $column) {
    //                 $column->row(function (Row $row) {
    //                     $row->column(6, new Examples\NewUsers());
    //                     $row->column(6, new Examples\NewDevices());
    //                 });

    //                 $column->row(new Examples\Sessions());
    //                 $column->row(new Examples\ProductOrders());
    //             });
    //         });
    // }
    public function index(Content $content): Content
    {
        return $content
            ->title('仪表盘')
            ->description('掌握你的一切资源')
            ->body(function (Row $row) {
                $row->column(3, new BannerMyAssetsWorth());
                // @permissions
                // if (Admin::user()->can('home.asset.admin')) {
                $row->column(3, new BannerMyTodoCounts());
                $row->column(3, new BannerMaintenanceRecordCounts());
                $row->column(3, new BannerServiceIssueCounts());
                // }
                // // @permissions
                // if (Admin::user()->can('home.dashboard')) {
                $row->column(12, '<hr>');
                $row->column(12, function (Column $column) {
                    $column->row(function (Row $row) {
                        $row->column(6, function (Column $column) {
                            $column->row(new CustomerTrend());
                            // $column->row(new DefectTrend());
                        });
                        $row->column(6, function (Column $column) {
                            $column->row(new OrderTrend());
                            // $column->row(new DefectTrend());
                        });
                        $row->column(12, function (Column $column) {
                            $column->row(new AssetsWorthTrend());
                        });
                    });
                });
                $data = MessageRecord::where('admin_id', Admin::user()->id)
                    ->get(['user_id', 'message_id', 'message_type', 'admin_id', 'created_at'])
                    ->toArray();

                foreach ($data as $key => &$val) {
                    $val['user_id'] = User::find($val['user_id'])->username ?? '';
                    if ($val['message_type'] == 1) {
                        $val['message_id'] = OrderServiceSubmitDetail::find($val['message_id'])->contents ?? ''; //OrderServiceSubmitDetail
                        $val['message_type'] = '投稿详情（投稿进度）';
                    } elseif ($val['message_type'] == 2) {
                        $val['message_id'] = OrderServiceProfile::find($val['message_id'])->contents ?? '';
                        $val['message_type'] = '补充资料';
                    }
                    $val['admin_id'] = AdminUser::find($val['admin_id'])->username ?? '';
                }
                $row->column(12, new Card('服务中订单', view('services_dashboard')
                    ->with('data', $data)));
            });
    }
}
